package com.company.zonghelizi.iodata;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * io 流 实现的 存储学生 的 数据库功能
 * 1. 建一个student.txt文件
 * 2. 读取文件信息
 * 3. 解析文件
 * 4. 创建Student类，放文件里的内容，姓名，性别，年龄
 * 5. 生成多个对象，放到ArrayList中，，这些文件的值来自来自文件
 * 6. 找到list中最大年龄的学生，开始改变性别
 * 7. 最后迭代List里的学生信息
 * 9. 把更新后的写入该文件
 */
public class Test {
    public static void main(String[] args) {
        List<Student> list = new ArrayList();
        try(        BufferedReader br = new BufferedReader(new FileReader("pkg/student.txt"));      ){
            String s;
            while ((s=br.readLine())!=null){
                String[] str = s.split("，");
                list.add(new Student(str[0],str[1],Integer.valueOf(str[2])));
            }
            Student stu = Collections.max(list);
            stu.setSet(stu.getSet().equals("男")?"女":"男");
            try(BufferedWriter bw = new BufferedWriter(new FileWriter("student.txt"))){
                for (Student ss:list) {
                    bw.write(ss.getName()+'，'+ss.getSet()+'，'+ss.getAge());
                    bw.newLine();
                }
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Student implements Comparable<Student>{
    private String name;
    private String set;
    private Integer age;

    public Student(String name, String set, Integer age) {
        this.name = name;
        this.set = set;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSet() {
        return set;
    }

    public void setSet(String set) {
        this.set = set;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student o) {
        return this.getAge() - o.getAge();
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", set='" + set + '\'' +
                ", age=" + age +
                '}';
    }
}
